<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
    $result=array("msg"=>"抱歉.暂不支持get请求","status"=>-1);
    print_r(json_encode($result));
    return;
}
$pid = $_REQUEST["pid"];
$imgUrl = $_REQUEST["imgUrl"];
$name = $_REQUEST["name"];
$price = $_REQUEST["price"];
$num = $_REQUEST["number"];
$color= $_REQUEST["colors"];
$title = $_REQUEST["title"];
$size2 = $_REQUEST["size2"];
$uid = $_REQUEST["uid"];
$total = $num * $price;
$size = $_REQUEST["size"];

$sizestr=json_encode($size);

//准备sql语句
include "DBhelper.php";
//1.是否已经购买过,
$sql = "SELECT * FROM `carslist` WHERE `pid`='".$pid."' AND `uid`='".$uid."' AND `size2`='".$size2."'";

$resultData=sqlQuery($sql);
//print_r($result);
if($resultData->num_rows>=1){
    $update="UPDATE `carslist` SET `number`=`number`+'".$num."',`total`=`number`*`price` WHERE `pid`='".$pid."' AND `uid`='".$uid."' AND `size2`='".$size2."'";
    $resultChange=sqlQuery($update);
    if($resultChange==1){
        $result=array("msg"=>"加入成功u","status"=>3);
        print_r(json_encode($result));
    }else{
        $result=array("msg"=>"未加入成功u","status"=>2);
        print_r(json_encode($result));
    }
} else {
    //如果没购买,插入一条记录
    $insert = " INSERT INTO `carslist` (`pid`,`name`,`price`,`url`,`size`,`color`,`total`,`number`,`uid`,`size2`,`title`) ";
    $insert .= "VALUES('".$pid."','".$name."','".$price."','".$imgUrl."','".$sizestr."','".$color."','".$num * $price."','".$num ."','".$uid."','".$size2."','".$title."') ";
//print_r($insert);
    $resultInsert = sqlQuery($insert); //更新的结果

    if ($resultInsert == 1) {
        $result=array("msg" => "加入成功i", "status" => 1);
        print_r(json_encode($result));
    } else {
        $result=array("msg" => "未加入成功i", "status" => -1);
        print_r(json_encode($result));
    }
}